// pages/cart/cart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Height: null,
    Width: null,
    total: 0,
    balance: 500,
    cartArr: [{
        id: 1,
        imgUrl: 'http://lengnuanit.top/images/components-image/new2.png',
        title: '三体: 死神永生',
        author: '刘慈欣',
        price: 55
      },
      {
        id: 2,
        imgUrl: 'http://lengnuanit.top/images/components-image/new4.png',
        title: '未来简史',
        author: '尤瓦尔• 郝拉利',
        price: 66
      },
      {
        id: 3,
        imgUrl: 'http://lengnuanit.top/images/orther/category/banner1.png',
        title: '银河帝国',
        author: '艾萨克·阿西莫夫',
        price: 99
      },
      {
        id: 4,
        imgUrl: 'http://lengnuanit.top/images/orther/category/banner2.png',
        title: '海底两万里',
        author: '儒勒·凡尔纳',
        price: 45
      },
      {
        id: 5,
        imgUrl: 'http://lengnuanit.top/images/orther/category/banner3.png',
        title: '神秘岛',
        author: '刘慈心',
        price: 59
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '购物车'
    })

    this.getWidthOrHeight()
    this.countTotal()
  },
  getWidthOrHeight() {
    const that = this
    wx.getSystemInfo({
      success(res) {
        that.setData({
          Height: res.windowHeight,
          Width: res.windowWidth
        })
      },
    })
  },
  // 计算总计
  countTotal() {
    let that = this
    let totalPrice = 0
    that.data.cartArr.map(item => {
      return totalPrice += item.price
    })
    that.setData({
      total: totalPrice
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})